//index.js
//获取应用实例
var WxSearch = require('../../wxSearch/wxSearch.js')
var app = getApp()
Page({
  data: {
//保存搜索框最终的值
    keyword: null
    
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    var hotWords;
    var data = []
    var list;

    wx.request({
      url:
      'https://www.magicmanufactory.com/c2mwebservice/api/v2/public/search/constant/hotwords/',

      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log("热门搜索的json" + JSON.stringify(res))

        list = res.data.returnData
        console.log(list[0].hotWords)
        for (var i = 0; i < list.length; i++) {
          console.log(list[i])
          data.push(list[i].hotWords)

        }
        //初始化的时候渲染wxSearchdata
        WxSearch.init(that, 43, data);

      }

    })
  },
 //点击搜索执行的方法
  wxSearchFn: function (e) {
    var that = this
    WxSearch.wxSearchAddHisKey(that);
    console.log("wxSearchFn方法触发")
    console.log(that.data.keyword)
    wx.navigateTo({
      url: '../goodsList/goodslist?keyWord=' + this.data.keyword,
    })
  

  },
  //检测到搜索框输入的方法
  wxSearchInput: function (e) {
    var that = this
    WxSearch.wxSearchInput(e, that);
    console.log("wxSearchInput触发")
    console.log("wxSearchInput的e" + JSON.stringify(e))
    that.setData({
      keyword: e.detail.value
    })
    console.log(that.data.keyword)
  },
//检测到搜索框获取到焦点的方法
  wxSerchFocus: function (e) {
    var that = this
    console.log(that.data.keyword)
    WxSearch.wxSearchFocus(e, that);
    console.log("wxSerchFocus触发")
    console.log("wxSerchFocus的e" + JSON.stringify(e))
  },
//输入完成执行的方法
  wxSearchBlur: function (e) {
    var that = this
    WxSearch.wxSearchBlur(e, that);
    console.log("wxSearchBlur触发")
    console.log("wxSearchBlur的e" + JSON.stringify(e))
    that.setData({
      keyword:e.detail.value
    })
    // wx.navigateTo({
    //   url: '../goodsList/goodslist?keyWord=' + that.data.keyword,
    // })
  },
  //点击提示关键字后执行的方法
  wxSearchKeyTap: function (e) {
    var that = this
    WxSearch.wxSearchKeyTap(e, that);
    console.log("wxSearchKeyTap触发")
    console.log("wxSearchKeyTap的e" + JSON.stringify(e))
    // that.setData({
    //   keyword: e.target.dataset.key
    // })
    let searchData= e.target.dataset.key
    wx.navigateTo({
      url: '../goodsList/goodslist?keyWord=' + searchData,
    })
  },
  wxSearchDeleteKey: function (e) {
    var that = this
    WxSearch.wxSearchDeleteKey(e, that);
    console.log("wxSearchDeleteKey触发")
  },
  wxSearchDeleteAll: function (e) {
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
    console.log("wxSearchDeleteAll触发")
  },
  wxSearchTap: function (e) {
    var that = this
    WxSearch.wxSearchHiddenPancel(that);
    console.log("wxSearchTap触发")
    console.log("wxSearchTap的e" + JSON.stringify(e))
    console.log("wxSearchTap参数传递的值为"+e.target.dataset.key)
    that.setData({
      keyword: e.target.dataset.key
    })
  }
})
